package com.cskaoyan.service.wx;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.model.admin.bo.BaseParam;
import com.cskaoyan.model.vx.vo.WxGoodsListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author hanshuai
 * @version 1.0
 * @description @TODO
 * @date 2021/12/2 21:32
 */
@Service
public class WxGoodsByHanServiceImpl implements WxGoodsByHanService {
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;

    @Override
    public WxGoodsListVo goodsList(BaseParam baseParam, String keyword, Integer categoryId, HttpSession httpSession) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
//        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
//        List<MarketCategory> marketCategories = new ArrayList<>();
//        if (categoryId==0){
//             marketCategoryMapper.selectByExample(marketCategoryExample);
//        }
        WxGoodsListVo wxGoodsListVo = new WxGoodsListVo();
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        if (keyword != null) {
            criteria.andNameLike("%" + keyword + "%");
        }
        if (categoryId != 0 && categoryId != null) {
            criteria.andCategoryIdEqualTo(categoryId);
            marketCategoryExample.createCriteria().andIdEqualTo(categoryId);
//            List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
//            wxGoodsListVo.setFilterCategoryList(marketCategories);
        }
        if (keyword != null) {
            MarketSearchHistory marketSearchHistory = new MarketSearchHistory();
            Date date = new Date();
            marketSearchHistory.setAddTime(date);
            marketSearchHistory.setDeleted(false);
            marketSearchHistory.setFrom("search");
            marketSearchHistory.setKeyword(keyword);
            String wxUserId =  httpSession.getAttribute("wxUserId").toString();
            if (wxUserId != null) {
                marketSearchHistory.setUserId(Integer.parseInt(wxUserId));
            } else {
                marketSearchHistory.setUserId(0);
            }
            marketSearchHistory.setUpdateTime(date);
            marketSearchHistoryMapper.insertSelective(marketSearchHistory);
        }
        if (baseParam.getOrder() != null && baseParam.getSort() != null) {
            marketGoodsExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        }
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
        List<MarketCategory> marketCategories = new ArrayList<>();
        for (MarketGoods marketGood : marketGoods) {
            MarketCategory marketCategory = marketCategoryMapper.selectByPrimaryKey(marketGood.getCategoryId());
            marketCategories.add(marketCategory);
        }
        PageInfo pageInfo = new PageInfo(marketGoods);
        wxGoodsListVo.setList(marketGoods);
        wxGoodsListVo.setFilterCategoryList(marketCategories);
        wxGoodsListVo.setLimit(baseParam.getLimit());
        wxGoodsListVo.setPage(baseParam.getPage());
        wxGoodsListVo.setPages(pageInfo.getPages());
        wxGoodsListVo.setTotal((int) pageInfo.getTotal());
        return wxGoodsListVo;
    }
}
